<template>
  <div class="systemtools">
    <el-header>
      <div class="head-left">
        <a href="#/proportal/index">
          <img
            src="@/assets/images/logo.png"
            alt="logo"
            width="28"
            height="28"
          />
        </a>
        <h1>管控台</h1>
      </div>
      <div class="head-right">
        <el-avatar
          :size="30"
          v-if="avatarInfo?.picture"
          :src="`/local/uploadFiles/${avatarInfo?.picture}`"
        ></el-avatar>
        <div
          v-else
          :class="['auto-avator', avatarInfo.sex === 0 ? 'girl' : 'boy']"
          slot="reference"
        >
          <span class="name">{{ avatarInfo.employee_name.slice(-2) }}</span>
        </div>
        <span class="name">{{ avatarInfo?.employee_name }}</span>
        <i class="el-icon-arrow-down"></i>
        <div id="selfDefine-message-box">
          <div class="topIcon"></div>
          <div class="content">个人中心</div>
          <div class="content logout" @click="logOut">退出登录</div>
        </div>
      </div>
    </el-header>
    <el-container>
      <el-aside
        :style="`width:${
          $store.state.isShowSider ? '240px' : '64px'
        };overflow:hidden;`"
      >
        <div class="wfc-menu">
          <el-menu
            :default-active="openNames[0]"
            :default-openeds="openNames"
            v-show="$store.state.isShowSider"
            :router="true"
            :collapse-transition="false"
            class="wfc-menu-opened scroll-content"
          >
            <MenuLeft :menuItem="menuList" />
          </el-menu>
          <div class="wfc-menu-closed" v-show="!$store.state.isShowSider">
            <menuLeftHide :menuItem="menuList" :siderMenuName="siderMenuName" />
          </div>
          <div class="show-sider" @click="$store.commit('setShowFlag')">
            <i :class="['iconfont', `icon-${collapseIcon()}`]"></i>
            <span v-if="$store.state.isShowSider">收起</span>
          </div>
        </div>
      </el-aside>
      <el-main class="scroll-content">
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import MenuLeft from "@/components/systemtools/MenuLeft.vue";
import menuLeftHide from "@/components/systemtools/MenuLeftHide";
import { getAllMenuCode, getLeftMenu } from "@/http/api";
export default {
  name: "systemtools",
  components: { MenuLeft, menuLeftHide },
  data() {
    return {
      avatarInfo: JSON.parse(localStorage.getItem("userInfo")),
      menuList: {},
      openNames: [],
      siderMenuName: "",
    };
  },
  async mounted() {
    let res = await getLeftMenu();
    if (res.code === 20041) {
      this.menuList = res.data.menu;
      let menu = [];
      for (const key in this.myFlat(res.data)) {
        let keys = key.split(".");
        if (keys[keys.length - 1] === "menu_code") {
          menu.push(this.myFlat(res.data)[key]);
        }
      }
      localStorage.setItem("menu", JSON.stringify(menu));
      localStorage.setItem("authority", JSON.stringify(res.data.authority));
    }
    res = await getAllMenuCode();
    if (res.code === 20041) {
      localStorage.setItem("allMenu", JSON.stringify(res.data));
    }
    const scrollView = document.querySelector(".wfc-menu-opened");
    let timer = {};
    scrollView.addEventListener("scroll", () => {
      if (timer) {
        clearTimeout(timer);
      }
      scrollView.classList.add("active-bar");
      timer = setTimeout(() => {
        scrollView.classList.toggle("active-bar");
      }, 1000);
    });
  },
  watch: {
    "$route.path": {
      handler() {
        this.openNames = [];
        let paths = this.$route.fullPath.split("/");
        let nowOpen = [];
        for (let index = 0; index < paths.length; index++) {
          if (paths[index] !== "") {
            nowOpen.push(paths[index].split("?")[0]);
          }
        }
        nowOpen.shift();
        this.openNames = nowOpen;
        this.siderMenuName = nowOpen[0];
      },
      immediate: true,
    },
  },
  methods: {
    collapseIcon() {
      return this.$store.state.isShowSider ? "ad_collapse" : "ad_expand";
    },
    logOut() {
      localStorage.removeItem("userInfo");
      localStorage.removeItem("cookie");
      const watermask = document.querySelector("#watermask");
      if (watermask) {
        document.body.className = "hiddenMask";
      }
      this.$router.push("/login");
    },
    myFlat(obj = {}, preKey = "", res = {}) {
      if (!obj) return;
      //获取obj对象的所有[key,value]数组并且遍历，forEach的箭头函数中用了解构
      Object.entries(obj).forEach(([key, value]) => {
        if (Array.isArray(value)) {
          //如果obj是数组，那么key就是数组的index，value就是对应的value
          //obj是数组的话就用[]引起来
          //因为value是数组，数组后面是直接跟元素的，不需要.号
          let temp = Array.isArray(obj)
            ? `${preKey}[${key}]`
            : `${preKey}${key}`;
          this.myFlat(value, temp, res);
        } else if (typeof value === "object") {
          //因为value是对象类型，所以在末尾需要加.号
          let temp = Array.isArray(obj)
            ? `${preKey}[${key}].`
            : `${preKey}${key}.`;
          this.myFlat(value, temp, res);
        } else {
          let temp = Array.isArray(obj)
            ? `${preKey}[${key}]`
            : `${preKey}${key}`;
          res[temp] = value;
        }
      });
      return res;
    },
  },
};
</script>

<style lang="less" scoped>
.systemtools {
  width: 100vw;
  height: 100vh;
  /deep/ .el-header {
    height: 64px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 24px;
    box-shadow: 0 2px 6px 0 hsl(0deg 0% 81% / 25%);
    position: relative;
    z-index: 1;
    .head-left {
      display: flex;
      align-items: center;
      h1 {
        font-size: 16px;
        line-height: 16px;
        padding-top: 2px;
        padding-left: 16px;
        font-weight: 700;
        color: rgba(0, 0, 0, 0.65);
      }
    }
    .head-right {
      display: flex;
      align-items: center;
      height: 100%;
      cursor: pointer;
      & > .name {
        font-size: 14px;
        padding: 0 6px;
      }
      i {
        font-size: 12px;
        color: #2b2f36;
      }
      #selfDefine-message-box {
        flex: none;
        position: absolute;
        box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
        padding: 6px 0;
        right: 30px;
        top: 54px;
        border: 1px solid #ebeef5;
        border-radius: 4px;
        width: 150px;
        opacity: 0;
        transition: opacity 0.3s;
        z-index: -1;
        background-color: #fff;
        .topIcon {
          position: absolute;
          top: -4px;
          right: 20%;
          width: 10px;
          height: 10px;
          background-color: #fff;
          border-left: 1px solid #ebeef5;
          border-top: 1px solid #ebeef5;
          transform: rotate(45deg);
        }
        .content {
          padding: 8px 12px 10px 18px;
          font-size: 14px;
          color: #606266;
          cursor: pointer;
          &.logout {
            border-top: 1px solid #e8eaed;
          }
          &:hover {
            background-color: #f5f6f7;
          }
        }
      }
      &:hover #selfDefine-message-box {
        opacity: 1;
        z-index: 1;
      }
    }
  }
  /deep/ .el-container {
    height: calc(100vh - 60px);
    .el-aside {
      border-right: 1px solid #e8eaed;
      .wfc-menu {
        width: 100%;
        .wfc-menu-opened {
          overflow-x: hidden;
          height: calc(100vh - 120px);
          border-right: solid 0 transparent;
          &::-webkit-scrollbar-thumb {
            display: none;
          }
          &.active-bar::-webkit-scrollbar-thumb {
            display: block;
          }
        }
        .wfc-menu-closed {
          width: 64px;
          overflow: hidden;
          height: calc(100vh - 120px);
        }
        .show-sider {
          padding: 20px 20px 0;
          img {
            vertical-align: middle;
          }
          span {
            padding-left: 10px;
            cursor: pointer;
            vertical-align: middle;
          }
          i {
            font-size: 20px;
            vertical-align: middle;
            cursor: pointer;
          }
        }
        .el-menu .el-menu-item {
          &.main_tab {
            line-height: 50px;
            margin-top: 10px;
          }
          svg {
            margin-right: 6px;
            height: 20px;
            width: 20px;
          }
          transition: all 0.1s;
          &:hover {
            background-color: transparent;
            color: #3c8cff;
            i {
              color: #3c8cff;
            }
          }
          &.is-active::before {
            content: "";
            width: 90%;
            height: 75%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(76, 120, 252, 0.15);
            color: #4c78fc;
            border-radius: 30px;
          }
        }
      }
    }
    .el-main {
      padding: 24px;
      height: calc(100vh - 60px);
      background-color: #f5f6fa;
      &.active-bar::-webkit-scrollbar-thumb {
        display: block;
      }
    }
  }
}
</style>
